package com.service.zk;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bean.zk.Log;
import com.bean.zk.Role;
import com.dao.zk.LogDAO;
import com.dao.zk.PowerDAO;
import com.dao.zk.RoleDAO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleService {

    @Resource
    private RoleDAO roleDAO;
    @Resource
    private LogDAO logDAO;
    @Resource
    private PowerDAO powerDAO;

    /**
     * 获得所有角色role
     * @return
     */
    public List<Map<String, Object>> getRoles() {
        return roleDAO.getRoles();
    }

    /**
     * 获得员工的所有角色
     * @param eid
     * @return
     */
    public List<Map<String, Object>> getRolesByEid(String eid) {
        return roleDAO.geRolesByEid(Integer.parseInt(eid));
    }

    /**
     * 根据员工id添加角色
     * @param eid
     * @param ids
     * @return
     */
    public int addRoleById(String eid, Object[] ids) {
        return roleDAO.addRoleById(Integer.parseInt(eid),ids);
    }

    /**
     * 根据员工id删除角色
     * @param eid
     * @param ids
     * @return
     */
    public int removeRoleById(String eid, Object[] ids) {
        return roleDAO.removeRoleById(Integer.parseInt(eid),ids);
    }

    /**
     * 根据角色id删除角色
     * @param rid
     * @return
     */
    public int delRoleById(String rid, HttpServletRequest request) {
        if(powerDAO.getPowersById(Integer.parseInt(rid)).size()!=0){
            return 0;
        }
        Role role = roleDAO.geRoleByRid(Integer.parseInt(rid));
        Map<String,Object> user = (Map<String,Object>)request.getSession().getAttribute("user");
        Object eid = user.get("eid");
        Log log = new Log();
        log.setEid((Integer) eid);
        log.setRemark("删除角色");
        log.setBefore(JSON.toJSONString(role));
        log.setAfter("");
        log.setLtype("delete");
//        执行插入方法，添加一条日志记录
        logDAO.insert(log);
       return roleDAO.delRoleById(Integer.parseInt(rid));
    }

    /**
     * 更新角色
     * role
     * @return
     */
    public int updateRoleByRid(Role newrole, HttpServletRequest request) {
        Map<String,Object> user = (Map<String,Object>)request.getSession().getAttribute("user");
        Role role = roleDAO.geRoleByRid(newrole.getRid());
        Object eid = user.get("eid");
        Log log = new Log();
        log.setEid((Integer) eid);
        log.setRemark("更新角色信息");
        log.setBefore(JSON.toJSONString(role));
        log.setAfter(JSON.toJSONString(newrole));
        log.setLtype("update");
//        执行插入方法，添加一条日志记录
        logDAO.insert(log);
       return roleDAO.updateRoleById(newrole.getRname(),newrole.getRemark(),newrole.getRid());
    }

    /**
     * 根据角色id查找角色
     * @param rid
     * @return
     */
    public Role getRole(String rid) {
       return roleDAO.geRoleByRid(Integer.parseInt(rid));
    }

    /**
     * 添加角色
     * @param rname
     * @param remark
     * @return
     */
    public int addRole(String rname, String remark, HttpServletRequest request) {
        int effect = roleDAO.addRole(rname,remark);

        Role role = roleDAO.geRoleByRname(rname);
        Map<String,Object> user = (Map<String,Object>)request.getSession().getAttribute("user");
        Object eid = user.get("eid");
        Log log = new Log();
        log.setEid((Integer) eid);
        log.setRemark("添加角色");
        log.setBefore("");
        log.setAfter(JSON.toJSONString(role));
        log.setLtype("insert");
//        执行插入方法，添加一条日志记录
        logDAO.insert(log);
        return effect;
    }


}
